package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.domain.mongo.Video;
import com.itheima.service.mongo.CommentService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public Integer saveMovementComment(Comment comment) {
        //1.处理comment对象
        comment.setCreated(System.currentTimeMillis());

        Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);

        comment.setPublishUserId(movement.getUserId());//设置被操作对象所属人

        //2.向评论表中保存数据
        mongoTemplate.save(comment);

        //3.向动态表中更新点赞次数
        if(comment.getCommentType()==1){
            //点赞+1
            movement.setLikeCount(movement.getLikeCount()+1);
        }else if(comment.getCommentType()==2){
            //评论+1
            movement.setCommentCount(movement.getCommentCount()+1);
        }else if(comment.getCommentType()==3){
            //喜欢+1
            movement.setLoveCount(movement.getLoveCount()+1);
        }
        mongoTemplate.save(movement);

        // 4.返回操作数量
        if (comment.getCommentType() == 1) {
            return movement.getLikeCount();
        } else if (comment.getCommentType() == 2) {
            return movement.getCommentCount();
        } else if (comment.getCommentType() == 3) {
            return movement.getLoveCount();
        } else {
            return 0;//实际不应该走到这
        }
    }

    @Override
    public Integer deleteMovementComment(Comment comment) {
        // 1.处理comment对象
            //条件：1.哪个动态下(publishId)  2.哪个评论内容(点赞1，喜欢3)  3.谁点的(userId)
        Query query = new Query(Criteria.where("publishId").is(comment.getPublishId())
                .and("commentType").is(comment.getCommentType())
                .and("userId").is(comment.getUserId()));

        // 2.从评论表中移除数据
        mongoTemplate.remove(query,Comment.class);

        // 3.向动态表中更新点赞次数
        Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
        if (comment.getCommentType() == 1) {
            movement.setLikeCount(movement.getLikeCount() - 1);
        } else if (comment.getCommentType() == 2) {
            movement.setCommentCount(movement.getCommentCount() - 1);
        } else if (comment.getCommentType() == 3) {
            movement.setLoveCount(movement.getLoveCount() - 1);
        }
        mongoTemplate.save(movement);

        //4. 返回操作数量
        if (comment.getCommentType() == 1) {
            return movement.getLikeCount();
        } else if (comment.getCommentType() == 2) {
            return movement.getCommentCount();
        } else if (comment.getCommentType() == 3) {
            return movement.getLoveCount();
        } else {
            return 0;
        }

    }

    @Override
    public PageBeanVo findMovementCommentList(String movementId, Integer pageNum, Integer pageSize) {
        //1.条件
        Query query = new Query(Criteria.where("publishId").is(new ObjectId(movementId)).and("commentType").is(2)).with(Sort.by(Sort.Order.desc("created"))).skip((pageNum - 1) * pageSize).limit(pageSize);
        //2.查询评论list
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        //3.统计数量
        long count = mongoTemplate.count(query, Comment.class);
        //4.返回pageBeanVo
        return new PageBeanVo(pageNum,pageSize,count,commentList);
    }

    @Override
    public PageBeanVo countCommentInfo(Long userId, Integer commentType, Integer pageNum, Integer pageSize) {
        //1. 根据动态发布人id和操作类型查询
        Query query = new Query(
                Criteria.where("publishUserId").is(userId)
                        .and("commentType").is(commentType)
        ).with(Sort.by(Sort.Order.desc("created")))
                .skip((pageNum - 1) * pageSize).limit(pageSize);

        List<Comment> commentList = mongoTemplate.find(query, Comment.class);

        //2. 统计
        long count = mongoTemplate.count(query, Comment.class);

        //3. 返回
        return new PageBeanVo(pageNum,pageSize,count,commentList);
    }

    //视频点赞
    @Override
    public Integer saveVideoComment(Comment comment) {
        //1.处理comment对象
        comment.setCreated(System.currentTimeMillis());
        Video video = mongoTemplate.findById(comment.getPublishId(), Video.class);
        comment.setPublishUserId(video.getUserId());//视频发布人



        //2.向评论表中保存数据
        mongoTemplate.save(comment);

        //3.向动态表更新点赞次数  4-对视频点赞   5-对视频评论    6-对评论点赞
        if(comment.getCommentType()==4){//点赞+1
            video.setLikeCount(video.getLikeCount()+1);
        }else if (comment.getCommentType()==5){
            video.setCommentCount(video.getCommentCount()+1);
        }
        /*else if (comment.getCommentType()==6){
            comment.setLikeCount(comment.getLikeCount()+1);
        }*/

        mongoTemplate.save(video);

        //返回操作数量
        if(comment.getCommentType()==4){
            return video.getLikeCount();
        }else if (comment.getCommentType()==5){
            return video.getCommentCount();
        }else{
            /* if (comment.getCommentType()==6){
           return comment.getLikeCount();
        }else {*/
            return 0;
        }
    }

    //取消视频点赞
    @Override
    public Integer deleteVideoComment(Comment comment) {
        //1.处理comment对象
        Query query = new Query();
        // 视频ID  视频点赞人ID 对视频做的操作
        query.addCriteria(Criteria.where("publishId").is(comment.getPublishId())
                .and("userId").is(comment.getUserId())
                .and("commentType").is(comment.getCommentType()));

        //从评论表删除数据
        mongoTemplate.remove(query,Comment.class);

        //动态表更改数据
        Video video = mongoTemplate.findById(comment.getPublishId(), Video.class);

        if(comment.getCommentType()==4){
            video.setLikeCount(video.getLikeCount()-1);
        }else if (comment.getCommentType()==5){
            video.setLoveCount(video.getLoveCount()-1);
        }

        mongoTemplate.save(video);
        //更新返回数据
        if(comment.getCommentType()==4){
            return video.getLikeCount();
        }else if (comment.getCommentType()==5){
            return video.getLoveCount();
        }else {
            return 0;
        }
    }

    //给视频评论点赞
    @Override
    public void saveVideoCommentLike(ObjectId publishId,Long userId) {

        //1.被点赞的评论的点赞数量+1:comment.getPublishId()
        Comment beLikeComment = mongoTemplate.findById(publishId, Comment.class);
        beLikeComment.setLikeCount(beLikeComment.getLikeCount()+1);
        mongoTemplate.save(beLikeComment);

        //2、保存该条评论到评论表
        Comment comment = new Comment();
        comment.setCommentType(6);
        comment.setUserId(userId);
        comment.setPublishId(publishId);
        comment.setPublishUserId(beLikeComment.getPublishUserId());
        comment.setCreated(System.currentTimeMillis());
        mongoTemplate.insert(comment);
    }

    //给视频评论点赞取消
    public void deleteVideoCommentLike(ObjectId publishId, Long userId) {
        //1.被点赞的评论的点赞数量+1:comment.getPublishId()
        Comment beLikeComment = mongoTemplate.findById(publishId, Comment.class);
        beLikeComment.setLikeCount(beLikeComment.getLikeCount()-1);
        mongoTemplate.save(beLikeComment);

        //2、删除对评论的点赞
        Query query = new Query();
        // 评论ID  评论点赞人ID 对评论做的操作
        query.addCriteria(Criteria.where("publishId").is(publishId)
                .and("userId").is(userId)
                .and("commentType").is(6));

        mongoTemplate.remove(query,Comment.class);
    }


}
